﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CustomerService
{
    public partial class tabPayment : System.Web.UI.UserControl
    {
        private SalesManagerEntities _context;
        protected tblOrderHeader _orderHeader = _emptyOrder;
        private static tblOrderHeader _emptyOrder = new tblOrderHeader();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                if (txtCardNumber.Text != string.Empty)
                {
                    tblAccount account;
                    if (_orderHeader.tblAccount == null || _orderHeader.tblAccount.GetAccountNumber(_context) != txtCardNumber.Text.Trim())
                    {
                        int?[] accountIDs = _context.spFindAccount(txtCardNumber.Text.Trim()).ToArray();

                        if (accountIDs.Length > 0)
                            account = _context.tblAccount.Find(new object[] { accountIDs[0] });
                        else
                            account = new tblAccount(_orderHeader);
                        _orderHeader.tblAccount = account;
                    }
                    else
                        account = _orderHeader.tblAccount;

                    if (txtCardNumber.Text.Trim() != account.GetAccountNumber(_context))
                    {
                        account.SetAccountNumber(_context, txtCardNumber.Text);
                    }
                    if (txtCardExpiration.Text.Trim() != (account.CardExpiration ?? string.Empty).Trim())
                        account.CardExpiration = txtCardExpiration.Text.Trim();
                }
            }
        }

        public new void Load(SalesManagerEntities context, tblOrderHeader orderHeader, bool forceUpdate)
        {
            _context = context;
            _orderHeader = orderHeader;

            if (!IsPostBack || forceUpdate)
            {
                if (_orderHeader.tblAccount != null)
                {
                    txtCardNumber.Text = _orderHeader.tblAccount.GetAccountNumber(_context);
                    txtCardExpiration.Text = _orderHeader.tblAccount.CardExpiration;
                }
                else
                {
                    txtCardNumber.Text = string.Empty;
                    txtCardExpiration.Text = string.Empty;
                }
            }
        }

        // Validate the the entered expiration date is in a valid format and it current.
        protected void OnValidateExpiration(object source, ServerValidateEventArgs args)
        {
            args.IsValid = true;
            try
            {
                DateTime dt = DateTime.Now;
                if (txtCardExpiration.Text.Trim() != string.Empty)
                    dt = DateTime.ParseExact(txtCardExpiration.Text.Trim(), "MMyy", System.Globalization.CultureInfo.CurrentCulture);
            }
            catch (Exception)
            {
                args.IsValid = false;
            }
        }
    }
}